/**
 * monitor_instances.java
 * ************************** 
 * @date Apr 16, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.scripts.monitorTools;

import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import infrastructureAPI.ConcreteRequester;
import infrastructureAPI.Requester;
import infrastructureAPI.APIMessages.ResponseMessage;
import infrastructureAPI.algorithms.monitorTools.MonitorInstances;
import infrastructureAPI.backendGate.APIRoutineFunctions;
import infrastructureAPI.backendGate.APIScript;
import infrastructureAPI.backendGate.GeneralScript;
import infrastructureAPI.backendGate.SharedAPIParameters;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.util.Vector;

import messages.Message;

/**
 * Description: 
 *
 */
public class monitor_instances extends GeneralScript {

	/**
	 * 
	 */
	public monitor_instances(String[] args) { 
		super(args);
	}

	// for debug only!!!
//	private monitor_instances(){	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		monitor_instances monitor = new monitor_instances(args);
		// the following is an equivalent way to launch the monitor_instances script
		// which take no advantage of the GeneralScript class.
//		new monitor_instances().executeScript(args);
	}
	
	public void executeScript(String[] args){
		String registeredServiceName = monitor_instances.class.getSimpleName();
		String name = "";
		int optionIndex = 0;
		InetAddress myAddress = null;
		boolean ret = false;
		
		System.out.println("REQUEST: "+registeredServiceName);
		
		// Translation of the input arguments
		// -----------------------
		Vector<String> arguments = APIRoutineFunctions.convertToVector(args);
		
		// Check of optional parameters
		// -----------------------
		if(arguments.contains(SharedAPIParameters.NODE_NAME_OPTION)){
			optionIndex = arguments.indexOf(SharedAPIParameters.NODE_NAME_OPTION);
			optionIndex++;
			name = ConcreteNodeDescriptor.adjustNodeName(arguments.get(optionIndex));
			optionIndex++;
		}else{
			try {
				name = InetAddress.getLocalHost().getHostName();
				name = ConcreteNodeDescriptor.adjustNodeName(name);
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		
		// Mandatory parameters verification
		// -----------------------
		
		// Mandatory parameters composition
		// -----------------------
		
		try {
			// Retrieve local address
			// -----------------------
			myAddress = InetAddress.getLocalHost();
			
			// Requester composition
			// -----------------------
			Requester me = new ConcreteRequester(myAddress, registeredServiceName, 
					APIRoutineFunctions.RMIREGISTRY_STADARD_PORT);
			
			// Instantiation of the APIScript: MonitorInstances
			// -----------------------
			APIScript  monitor = new MonitorInstances(me);
			
			// ResponseReceiver initialization
			// -----------------------
			APIRoutineFunctions.initializeResponseReceiver(myAddress, registeredServiceName);
			
			// RequestMessage: Marshaling
			// -----------------------
			Message request =  APIRoutineFunctions.marshal(myAddress, name, monitor);
			
			// RequestMessage: Delivering
			// -----------------------
			System.out.println("delivering request message: "+request+"\nfrom node: "+name);
			ret = APIRoutineFunctions.deliverMessage(request, name);
			if(ret){
				System.out.println("SUCCESSFUL delivering!");
			}else{
				System.out.println("FAILED delivering");
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		// Waiting for incoming replies
		// -----------------------
		ResponseMessage respMsg = APIRoutineFunctions.receiveResponse();
		System.out.println("Received answer: "+respMsg);
		
		// ResponseReceiver disconnection
		// -----------------------
		APIRoutineFunctions.disconnectReceiver();
	}

	
	@Override
	public void checkNotGeneralOptionalParameters() {
		// nothing to do
		// in this script there are other optional parameters 
	}

	@Override
	public void composeMandatoryParameters() {
		// nothing to do
		// in this script there aren't any other mandatory parameter
	}

	@Override
	public APIScript instantiateAPIScript() {
		return new MonitorInstances(requester);
	}

	@Override
	public boolean verifyMandatoryParameters() {
		// no verification needed
		return true;
	}

	@Override
	public void waitingIncomingResposes() {
		ResponseMessage respMsg = APIRoutineFunctions.receiveResponse();
		System.out.println("Received answer: "+respMsg);
	}

}
